import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';

import { SellLuckdrawManageService } from '../sell-luckdraw-manage.service';

@Component({
  selector: 'app-order-lottery-list',
  templateUrl: './order-lottery-list.component.html',
  styleUrls: ['./order-lottery-list.component.scss']
})
export class OrderLotteryListComponent implements OnInit {

  seachParams: any = {
    activityName: '',
    status: '' // 状态 0.未开始 1.进行中 2.已删除 3已结束 4 禁用
  };

  tableData: any = [];
  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private router: Router,
    private message: NzMessageService,
    private services: SellLuckdrawManageService
  ) { }

  ngOnInit(): void {
    this.loadTable();
  }

  async loadTable() {
    try {
      let params = {
        ...this.seachParams,
        page: this.tableParams.page,
        pageSize: this.tableParams.pageSize
      }
      this.tableParams.tableLoading = true;
      const res: any = await this.services.queryLotteryList(params);
      this.tableParams.tableLoading = false;

      if (res.code == 0) {
        const { records, total } = res.data || {};
        this.tableData = records || [];
        this.tableParams.total = total || 0;
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.tableParams.tableLoading = false;
      this.tableData = [];
      this.tableParams.total = 0;
      this.message.error(error);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData() {
    for (let key in this.seachParams) {
      this.seachParams[key] = '';
    }

    this.tableParams.page = 1;
    this.loadTable();
  }

  /**
   * 页面跳转
   * @param num 0：新增，1：编辑，2：复制，3：记录页面
   */
  jumpPage(num: number, item?: any) {
    switch (num) {
      case 0:
        this.router.navigate(['/sell/orderLotteryConfig'], {
          queryParams: {
            type: num
          }
        })
        break;
      case 1:
      case 2:
        // console.log('编辑');
        this.router.navigate(['/sell/orderLotteryConfig'], {
          queryParams: {
            type: num,
            id: item.id
          }
        })
        break;
      case 3:
        // console.log('记录');
        this.router.navigate(['/sell/orderLotteryRecord'])
        break;
    }
  }

  // 启用 | 禁用
  async enableDisable(item: any, num: number) {
    try {
      let params = {
        id: item.id,
        status: num
      }
      const res: any = await this.services.updateStatusItem(params);

      if (res.code == 0) {
        this.message.success((num == 0 ? '启用' : '禁用') + '成功');
        this.loadTable();
      } else {
        this.message.error(res.message || '操作失败');
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
